$(document).ready(function() {
    $.fn.dataTableExt.sErrMode = 'throw';

    getAllUsulan();

    $.getJSON('controller/cont.cuti.php',{get_ref_cuti:true},function(data){
        $.each(data,function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.kode).text(data.uraian); 
            $('#id_cuti_peg').append(option2);
        });
    });

    $('#btn_simpan_usulan').click(function(data) {
        var no_surat = $('#no_surat').val();
        var tgl_surat = $('#tgl_surat').val();
        var perihal = $('#perihal_surat').val();
        if (no_surat === '' || tgl_surat === '' || perihal === '') {
            $('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.usulan.php', {aksi_rekam_usulan: true, no_surat: no_surat, tgl_surat: tgl_surat, perihal: perihal}, function(data) {
                if (data.msg) {
                    $('#success').html('Data Berhasil Direkam').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input_usulan();
                } else {
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }, 'json');
            getAllUsulan();
        }
        getAllUsulan();
        return false;
    });


    $('#btn_simpan_ubah_usulan').click(function(data) {
        var id_usulan = $('#id_usulan').val();
        var no_surat = $('#no_surat').val();
        var tgl_surat = $('#tgl_surat').val();
        var perihal = $('#perihal_surat').val();
        if (no_surat === '' || tgl_surat === '' || perihal === '') {
            $('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.usulan.php', {aksi_simpan_ubah_usulan: true, id_usulan: id_usulan, no_surat: no_surat, tgl_surat: tgl_surat, perihal: perihal}, function(data) {
                if (data.msg) {
                    $('#success').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input_usulan();
                } else {
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }, 'json');
        }
        getAllUsulan();
        return false;
    });

//    $('#btn_tambah_pegawai').click(function(e) {
//        e.preventDefault();
//        $('#frm_tambah_pegawai').fadeIn(500);
//    });

    $('#btn_simpan_peg').click(function(e) {
        e.preventDefault();
        var id_usulan = $('#id_usulan_peg').val();
        var nip = $('#nip_peg').val();
        var e = document.getElementById('id_cuti_peg');
        var id_cuti = e.options[e.selectedIndex].value;
        var f = document.getElementById('id_tujuan_peg');
        var id_tujuan = f.options[f.selectedIndex].value;
        var tgl_mulai = $('#tgl_mulai').val();
        var tgl_selesai = $('#tgl_selesai').val();
        if (nip === '' || tgl_mulai === '' || tgl_selesai === '') {
            $('#error').html('Tidak Boleh Kosong').fadeIn(500).delay(2500).fadeOut(500);
        }else if(tgl_mulai> tgl_selesai){
            $('#error').html('Tgl awal cuti harus lebih awal dr tgl akhir cuti').fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.detail.php', {aksi_rekam_detail: true, id_usulan: id_usulan, nip: nip,
                id_cuti: id_cuti, id_tujuan: id_tujuan, tgl_mulai: tgl_mulai, tgl_selesai: tgl_selesai}, function(data) {
                if (data.msg) {
                    $('#success5').html('Data Berhasil Disimpan').fadeIn(500).delay(2500).fadeOut(500);
                    get_daftar_pegawai(id_usulan)
                    clear_input_peg();
                } else {
                    $('#error5').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }, 'json');
        }


        return false;


    });


    $('#btn_simpan_ubah_peg').click(function(e) {
        e.preventDefault();

        var id_det_usulan = $('#id_det_usulan').val();
        var id_usulan = $('#id_usulan_peg').val();
        var nip = $('#nip_peg').val();
        var e = document.getElementById('id_cuti_peg');
        var id_cuti = e.options[e.selectedIndex].value;
        var f = document.getElementById('id_tujuan_peg');
        var id_tujuan = f.options[f.selectedIndex].value;
        var tgl_mulai = $('#tgl_mulai').val();
        var tgl_selesai = $('#tgl_selesai').val();

        if (nip === '' || tgl_mulai === '' || tgl_selesai === '') {
            $('#error5').html('Semua Field Harus Diisi').fadeIn(500).delay(2500).fadeOut(500);
        }else if(tgl_mulai> tgl_selesai){
            $('#error5').html('Tgl awal cuti harus lebih awal dr tgl akhir cuti').fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.detail.php', {aksi_simpan_ubah_peg: true, id_det_usulan: id_det_usulan, nip: nip,
                id_cuti: id_cuti, id_tujuan: id_tujuan, tgl_mulai: tgl_mulai, tgl_selesai: tgl_selesai}, function(data) {
                if (data.msg) {
                    $('#success5').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
                    get_daftar_pegawai(id_usulan)
                    clear_input_peg();
                } else {
                    $('#error5').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }, 'json');
        }

    });


    $("#nip_peg").keyup(function() {
        var val=$('#nip_peg').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nip_td').innerHTML=data.nip;
                    document.getElementById('nama_td').innerHTML=data.nama;
                    document.getElementById('gol_td').innerHTML=data.golongan+" / "+data.pangkat;
                    document.getElementById('eselon_td').innerHTML=data.eselon;
                    $('#biodata').fadeIn(500).delay(10000).fadeOut(500);
                }else{
                    document.getElementById('biodata').innerHTML='NIP Tidak Ada di Daftar';
                    $('#biodata').fadeIn(500).delay(10000).fadeOut(500);
                }
                
            });
        }   
    });


    return false;
});
function cek_status(id_usulan){
    $.getJSON('controller/cont.usulan.php',{aksi_cek:true,id_usulan:id_usulan},function(data){
        var output;
        output=data;
        return output;
    });
}

function ajaxProsesUpload(){
    var alasan=$('#alasan').val();
    var id_det_usulan=$('#id_detail').val();
    var e = document.getElementById('persetujuan');
    var id_persetujuan = e.options[e.selectedIndex].value;

    $.ajaxFileUpload({
        url: 'controller/cont.proses_upload.php',
        secureuri: false,
        fileElementId: 'file_surat',
        dataType: 'json',
        data: {id_det_usulan: id_det_usulan,alasan: alasan,id_persetujuan: id_persetujuan},
        success: function(data, status){
            if(data.msg){
                $('#success6').html('Proses berhasil dilakukan').fadeIn(500).delay(2500).fadeOut(500);
            }else{
                $('#error6').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
            }
        }
    });
    return false;
}

function ajaxFileUpload(){
    var id_usulan = $('#id_usulan_upload').val();
    var judul_file = $('#judulFile').val();

        $.ajaxFileUpload({
            url: 'controller/cont.upload.php',
            secureuri: false,
            fileElementId: 'file_dokumen',
            dataType: 'json',
            data: {id_usulan: id_usulan, judul: judul_file},
            success: function(data, status)
            {
                if(data.msg){
                    $('#success3').html('File Berhasil Diupload').fadeIn(500).delay(2500).fadeOut(500);
                    get_dokumen(id_usulan);
                }else{
                    $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }
        });
        return false;
}

function clear_upload(){
    document.getElementById('judul_file').value = "";
    document.getElementById('file_dokumen').value = "";
}

function batal_input() {
    clear_input_peg()
}

function clear_input_peg() {

    document.getElementById('nip_peg').value = "";
    document.getElementById('tgl_mulai').value = "";
    document.getElementById('tgl_selesai').value = "";
    $('#btn_simpan_peg').show();
    $('#btn_simpan_ubah_peg').hide();
}

function clear_input_usulan() {
    $('#modale').show();
    $('#modale2').hide();
    $('.modal .modal-dialog').css({
        'width': '40%'
    });
    $('#judul_modal').html('Rekam Usulan');
    $('#btn_simpan_usulan').show();
    $('#btn_simpan_ubah_usulan').hide();
    document.getElementById('id_usulan').value = "";
    document.getElementById('no_surat').value = "";
    document.getElementById('tgl_surat').value = "";
    document.getElementById('perihal_surat').value = "";
}

function ubah_usulan(id_usulan) {
    $('#modale').show();
    $('#modale2').hide();
    $('#judul_modal').html('Ubah Usulan');
    $('#btn_simpan_usulan').hide();
    $('#btn_simpan_ubah_usulan').show();
    $.getJSON('controller/cont.usulan.php', {aksi_get_usulan_ubah: true, id_usulan: id_usulan}, function(data) {
        if (data.msg) {
            document.getElementById('id_usulan').value = data.id_usulan;
            document.getElementById('no_surat').value = data.no_surat;
            document.getElementById('tgl_surat').value = data.tgl_surat;
            document.getElementById('perihal_surat').value = data.perihal;
        } else {
            $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function getAllUsulan() {
    $.getJSON('controller/cont.usulan.php', {list_usulan: true}, function(data) {
        $('#list_usulan tbody').html('');
        var tab="";
        if(jQuery.isEmptyObject(data)){
            $('#list_usulan tbody').append('<td colspan="9">Data Kosong</td>');  
        }else{
            $.each(data, function(index, data) {
                tab = "<tr>";
                tab += "<td>" + data.no_surat +" <span class=\"label label-"+data.label+"\">"+data.status+"</span></td>";               
                tab += "<td>" + data.tgl_surat + "</td>";
                tab += "<td>" + data.jml + "</td>";
                //tab += "<td>" + data.kanwil + "</td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"uploadDokumen(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";
                tab += "<td align=\"center\"><button type=\"button\" onclick=\"hapus_usulan(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"ubah_usulan(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-edit\"></span>&nbsp;Ubah</a></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"insert_pegawai(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Daftar Pegawai</a></td>";
                tab += "<td align=\"center\"><button onclick=\"kirim_usulan(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Kirim</button></td>";
                tab += "</tr>";
                //alert(tab);
                $('#list_usulan tbody').append(tab);
            });
                $('#list_usulan').dataTable({
                    "sPaginationType": "bs_full",
                    "bInfo": false,
                    "bLengthChange": false,
                    "iDisplayLength": 5,
                  }); 
                  $('#list_usulan').each(function(){
                    var datatable = $(this);
                    // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                    var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                    search_input.attr('placeholder', 'Search');
                    search_input.addClass('form-control input-sm');
                    // LENGTH - Inline-Form control
                    var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                    length_sel.addClass('form-control input-sm');
                  });

        }
    });
    clear_input_peg();
}

function insert_pegawai(id_usulan) {
    $('#modale').hide();
    $('#modale2').show();
    $('.modal .modal-dialog').css({
        'width': '65%'
    });
    clear_input_peg();
    get_daftar_pegawai(id_usulan);

}

function hapus_usulan(id_usulan) {
    $.getJSON('controller/cont.usulan.php', {aksi_hapus_usulan: true, id_usulan: id_usulan}, function(data) {
        if (data.msg) {
            alert("Usulan berhasil dihapus");
            getAllUsulan();
        } else {
            $('#error2').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function get_daftar_pegawai(id_usulan) {
    document.getElementById('id_usulan_peg').value = id_usulan;
    $.getJSON('controller/cont.usulan.php', {aksi_daftar_pegawai: true, id_usulan: id_usulan}, function(data) {
        $('#list_pegawai tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.daftar, function(index, daftar) {

                tab = "<tr>";
                tab += "<td>" + daftar.nip + "</td>";
                tab += "<td>"+daftar.nama_peg+"</td>";
                tab += "<td>" + daftar.id_cuti + "</td>";
                tab += "<td>" + daftar.id_tujuan + "</td>";
                tab += "<td>" + daftar.tgl_mulai + " s.d " + daftar.tgl_selesai + "</td>";
                tab += "<td>"+daftar.level+"</td>";
                tab += "<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-sm\" onclick=\"hapus_peg(" + daftar.id_det_usulan + ")\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" href=\"#myModalInsertPegawai\" onclick=\"ubah_peg(" + daftar.id_det_usulan + ")\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
                // tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" href=\"#myModalProses\" onclick=\"proses_ijin("+daftar.id_det_usulan+")\" ><span class=\"glyphicon glyphicon-saved\"></span>Proses</a></td>";
                tab += "</tr>";
                //alert(tab);

                $('#list_pegawai tbody').append(tab);
            });
            //$("#list_pegawai").dataTable().fnDestroy();
            
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_pegawai tbody').append(tab);
        }

         $('#list_pegawai').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
              }); 
              $('#list_pegawai').each(function(){
                var datatable = $(this);
                // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                search_input.attr('placeholder', 'Search');
                search_input.addClass('form-control input-sm');
                // LENGTH - Inline-Form control
                var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                length_sel.addClass('form-control input-sm');
              });
              

    });
}

function ubah_peg(id_det) {
    $.getJSON('controller/cont.detail.php', {aksi_get_det_peg: true, id_usulan_peg: id_det}, function(data) {
        $('#btn_simpan_peg').hide();
        $('#btn_simpan_ubah_peg').show();

        document.getElementById('nip_peg').value = data.nip;
        document.getElementById('tgl_mulai').value = data.tgl_mulai;
        document.getElementById('tgl_selesai').value = data.tgl_selesai;
        document.getElementById('id_det_usulan').value = data.id_det_usulan;
        document.getElementById('id_usulan_peg').value = data.id_usulan;
        var ddl = document.getElementById('id_cuti_peg');
        var opts = ddl.options.length;
        for (var i = 0; i < opts; i++) {
            if (ddl.options[i].value == data.id_cuti) {
                ddl.options[i].selected = true;
                break;
            }
        }

        var ddl2 = document.getElementById('id_tujuan_peg');
        var opts2 = ddl2.options.length;
        for (var i = 0; i < opts2; i++) {
            if (ddl2.options[i].value == data.id_tujuan) {
                ddl2.options[i].selected = true;
                break;
            }
        }

    });
}

function hapus_peg(id_detail) {
    $.getJSON('controller/cont.detail.php', {aksi_hapus_det: true, id_det_usulan: id_detail}, function(data) {
        if (data.msg) {
            $('#success').html('Data Berhasil Dihapus').fadeIn(500).delay(2500).fadeOut(500);
            get_daftar_pegawai(data.id_usulan);
        } else {
            $('#error').html('Data gagal dihapus').fadeIn(500).delay(2500).fadeOut(500);
        }

    });

}

function uploadDokumen(id_usulan) {
    document.getElementById('id_usulan_upload').value = id_usulan;
    
    get_dokumen(id_usulan);
}


function get_dokumen(id_usulan){
    $.getJSON('controller/cont.dokumen.php',{aksi_list_dokumen:true,id_usulan:id_usulan},function(data){
        $('#list_dokumen tbody').html('');
        var tab='';
        var i=1;
        if(data.msg){
            $.each(data.list_file,function(index,list_file){
                tab += "<tr>";
                tab += "<td>"+i+"</td>";
                tab += "<td>"+list_file.judul+"</td>";
                tab += "<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-sm\" onclick=\"hapusFile("+list_file.id_dokumen+")\" ><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" onclick=\"download_file("+list_file.id_dokumen+")\"><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Lihat</a></td>";
                tab += "</tr>";
                i++;
            });
            $('#list_dokumen tbody').append(tab);
        }else{
            tab="<tr><td colspan=\"3\">Dokumen Kosong, Silahkan Upload File Anda</td></tr>";
            $('#list_dokumen tbody').append(tab);
        }
    });
}

function download_file(id_dokumen){
    $.getJSON('controller/cont.dokumen.php',{aksi_download:true,id_dokumen:id_dokumen},function(data){
        if(data.msg){  
            window.open(data.url,'Download');
        }else{
            $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    })  
}

function hapusFile(id_dokumen){
    $.getJSON('controller/cont.dokumen.php',{aksi_hapus_file:true,id_dokumen:id_dokumen},function(data){
        if(data.msg){
            $('#success3').html('Data Berhasil Dihapus').fadeIn(500).delay(2500).fadeOut(500);
            get_dokumen(data.id_usulan);
        }else{
            $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function proses_ijin(id_detail){
    $.getJSON('controller/cont.proses.php',{aksi_proses_usulan:true,id_det_usulan:id_detail},function(data){
        if(data.msg){
            document.getElementById('nip').innerHTML=data.nip;
            document.getElementById('nama').innerHTML=data.nama;
            document.getElementById('gol').innerHTML=data.golongan+" / "+data.pangkat;
            document.getElementById('eselon').innerHTML=data.eselon;
            document.getElementById('id_tujuan').innerHTML=data.id_tujuan;
            document.getElementById('id_cuti').innerHTML=data.id_cuti;
            document.getElementById('tgl').innerHTML=data.tgl_mulai+" s.d. "+data.tgl_selesai;
            document.getElementById('id_detail').value=id_detail;
            if(data.id_tanggapan!==""){
                alert(data.id_tanggapan);
            }else{
                //$('#form_proses').show();
            }
        }
    });
}

// function getPegawai(nip){
//     $.getJSON('controller/cont.pegawai.php',{aksi_nip:true,nip:nip},function(data){
//         if(data.msg){
            
//         }else{
            
//         }
//     })
// }

function kirim_usulan(id_usulan){
    $.getJSON('controller/cont.usulan.php',{aksi_kirim_usulan:true,id_usulan:id_usulan},function(data){
        if(data.msg){
            $('#success2').html('Usulan Berhasil Dikirim').fadeIn(500).delay(2500).fadeOut(500);
        }else{
            $('#error2').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

